/**
 */

import java.util.Random;


/**
 * @author Araya Zaesim, Stanley Ayzenberg, Michelle Partogi, Adam Perez
 * @version 1.0
 */
public class MarketPlaceLogic {
	/*
	 * When arriving at a planet, you need to compute the price of goods at that
	 * planet. Look at the planets tech level (say it is Medieval = tech level
	 * 2). You could not buy or sell Machines or Robots here. The price for
	 * water would be 30 (the base price) + 3*2 (the IPL * (Planet Tech Level -
	 * MTLP)) + (variance). We compute the variance by flipping a coin say heads
	 * price is increased, tails decreased. Since the water variance is 4, we
	 * would pick a random number 0-4. In our example, say we threw heads and
	 * got a 2. Our final price would be:
	 * 
	 * 30 + (3*2) + (30*.02) = 30 + 6 + 6 = $42
	 * 
	 * MTLP = Minimum Tech Level to Produce this resource (You can't buy on
	 * planets below this level) MTLU = Minimum Tech Level to Use this resource
	 * (You can't sell on planets below this level)
	 */

	/**
	 * toString method.
	
	 * @return String */
	public String toString() {
		return "";
	}
	
	/**
	 * Field system.
	 */
	protected SolarSystem system = null;

	/**
	 * Field event.
	 */
	protected String event = null;

	/**
	 * Field generator.
	 */
	private final Random generator;

	/**
	 * Field buyArray.
	 */
	private final int[] buyArray;
	
	/**
	 * Number of goods.
	 */
	private static final int NUM_GOODS = 10;
	
	/**
	 * Randomizer.
	 */
	private static final int RANDONE = 4;
	
	/**
	 * Randomizer.
	 */
	private static final int RANDTWO = 7;
	
	/**
	 * Price adjuster
	 */
	private static final int PRICEADJUSTER = 10;
	
	/**
	 * Multiplier
	 */
	private static final int MULTIPLIER = 2;
	
	/**
	 * Selling multiplier
	 */
	private static final int SELLINGMULT = 6;
	
	/**
	 * Conditional selling multiplier
	 */
	private static final int SELLINGMULTTWO = 18;
	
	/**
	 * Selling price adjuster
	 */
	private static final int PRICEADJ_SELL = 20;
	
	/**
	 * Selling multiplier conditional two.
	 */
	private static final int SELLING_SECOND = 7;
	
	/**
	 * Price randomizer
	 */
	private static final int PRICE_RAND = 4;
	
	/**
	 * Constructor for MarketPlaceLogic.
	 * @param system SolarSystem
	 * @param event String
	 */
	public MarketPlaceLogic(SolarSystem system, String event) {
		this.system = system;
		this.event = event;
		this.generator = new Random();
		buyArray = new int[NUM_GOODS];
	}

	/**
	 * Method buyPrices.
	
	 * @return int[] */
	public int[] buyPrices() {
		int index = 0;
		final int[] array = new int[NUM_GOODS];
		for (Goods good : Goods.values()) {
			if (good.mtlp <= system.getTechLevel()) {
				int var = generator.nextInt(RANDONE);
				int normalPrice = good.baseprice
						+ (good.ipl * (system.getTechLevel() - good.mtlp))
						+ var;
				if (good.cr.equals(system.getResource())) {
					array[index] = (RANDTWO * normalPrice / PRICEADJUSTER);
				} else if (good.er.equals(system.getResource())) {
					array[index] = MULTIPLIER * normalPrice;
				} else {
					array[index] = normalPrice;
				}

				if (good.ie.equals(event)) {
					System.out.println(event + " price of " + good.name()
							+ " increased!");
					array[index] = array[index] << 1;
				}
			} else {
				array[index] = 0;
			}
			buyArray[index] = array[index];
			index++;
		}// end for
		return array; // if price is zero, don't show it

	}// end buyPrices

	/**
	 * Method sellPrices.
	
	 * @return int[] */
	public int[] sellPrices() {
		int index = 0;
		final int[] array = new int[NUM_GOODS];
		for (Goods good : Goods.values()) {

			if (good.mtlu <= system.getTechLevel()) {
				int var = generator.nextInt(RANDONE);
				int normalPrice = good.baseprice
						+ (good.ipl * (system.getTechLevel() - good.mtlu))
						+ var;
				if (good.cr.equals(system.getResource())) {
					array[index] = (SELLINGMULT * normalPrice / PRICEADJUSTER);
				} else if (good.er.equals(system.getResource())) {
					array[index] = (SELLINGMULTTWO * normalPrice / PRICEADJ_SELL);
				} else {
					array[index] = normalPrice;
				}

				if (good.ie.equals(event)) {
					array[index] = (array[index] * SELLINGMULTTWO / PRICEADJ_SELL);
				}

				while (array[index] > buyArray[index]) {
					array[index] = (array[index] * SELLING_SECOND / PRICEADJUSTER);
				}
			} else {
				array[index] = 0;
			}
			index++;
		}// end for

		return array; // if price is zero, don't show it

	}// end sell

	/*
	 * Calculates price of particular good when trading with trade Must change
	 * System to new SolarSystem before making this call and create a new
	 * MarketPlaceLogic
	 */
	/**
	 * Method calcTraderPrice.
	 * @param good Goods
	
	 * @return int */
	public int calcTraderPrice(Goods good) {
		final int var = generator.nextInt(PRICE_RAND);
		return good.baseprice
				+ (good.ipl * (system.getTechLevel() - good.mtlu)) + var;
	}
}
